package cn.android.support.v7.lib.sin.crown.dialog;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;

import cn.android.support.v7.lib.sin.crown.R;
import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;
import cn.android.support.v7.lib.sin.crown.utils.ToastUtils;
import cn.android.support.v7.lib.sin.crown.view.ProgressView;


/**
 * 圆形进度条【需要手动关闭，返回键无法关闭】
 * Created by 彭治铭 on 2017/5/21.
 */

public class ProgressDailog extends BaseDialog {

    //在子线程中，显示出来
    public static void buider(final Activity activity) {
        if (activity != null && !activity.isFinishing()) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    getInstance(activity);
                }
            });
        }
    }

    //在子线程中关闭窗口
    public static void tear(final Activity activity) {
        if (activity != null && !activity.isFinishing()) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (!isfinish(activity, progressDailog)) {
                        progressDailog.dismiss();
                    }
                }
            });
        }
    }

    public static boolean isAttach = true;
    public static ProgressDailog progressDailog;

    public static ProgressDailog getInstance(Activity activity) {
        //在主线程中实例化。防止子线程中调用错误。
        if (isfinish(activity, progressDailog)) {
            progressDailog = new ProgressDailog(activity);
        }
        return progressDailog;
    }

    public ProgressView progressView;

    public ProgressDailog(Activity activity) {
        if (activity != null) {
            init(activity, R.layout.crown_dialog_progress);
        }
    }

//    //防止网络连接时间超时，自动取消弹出框。
//    public Handler handler = new Handler(Looper.getMainLooper()) {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            //Log.e("test", "是否显示:\t" + dialog.isShowing());
//            removeMessages(0);
//            if (dialog != null && activity != null && !activity.isFinishing() && dialog.isShowing()) {
////                long s = System.currentTimeMillis() - start;
////                long e = s / 1000;
//                //Log.e("test", "弹出框定时器:\t" + e);
////                if (e > 8) {
////                    ToastUtils.showToastView("请求超时,请稍后再试");
////                    dismiss();
////                }
//                dismiss();
//            }
//        }
//    };

    long start;

    @Override
    protected void initUI() {
        progressView = (ProgressView) findViewById(R.id.crown_progressView);
    }

    @Override
    protected void adapterUI() {
        ProportionUtils.getInstance().adapterScreen(activity, findViewById(R.id.crown_progress_dialog));
    }

    @Override
    protected void setWindowAnimations() {
        //super.setWindowAnimations();
        window.setWindowAnimations(R.style.crown_window_alpha);
    }

    @Override
    protected boolean isAutoAdapterUI() {
        return false;
    }

    @Override
    protected boolean isStatus() {
        return true;
    }

    @Override
    protected boolean isTransparent() {
        return false;
    }

    @Override
    protected boolean isLocked() {
        return true;
    }

    @Override
    protected void listener() {
        if (activity != null) {
            isAttach = true;
            progressView.degress = 0;
            progressView.setVisibility(View.VISIBLE);
            start = System.currentTimeMillis();
            //handler.removeMessages(0);
            //handler.sendEmptyMessageDelayed(0, 20000);//定时20秒,如果超过20秒钟，弹出框自动消失。
            //handler.sendEmptyMessageDelayed(0, 180000);//3分钟
        }
    }

    @Override
    protected void recycleView() {
        if (activity != null) {
            isAttach = false;
            progressView.setVisibility(View.INVISIBLE);
            //handler.removeMessages(0);
        }
    }

    public static void setIsAttach(boolean isAttach) {
        ProgressDailog.isAttach = isAttach;
    }

    public static void setProgressDailog(ProgressDailog progressDailog) {
        ProgressDailog.progressDailog = progressDailog;
    }

    public void setProgressView(ProgressView progressView) {
        this.progressView = progressView;
    }

    public void setStart(long start) {
        this.start = start;
    }
}
